const app = getApp();
import { jiatuiRequest, zg99Request } from "/utils/requests";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showContact: true,
    suspendShow: true,
    showIcon: true,
    bgcolor: "",
    options: {
      s1: "y",
      xiangmu: "sk",
      lx: "sk",
      selectNo: "y",
    },
    yl: 1,
    lx: "sk",
    myPhone: "",
    cxlists: [],
    chaxunjg: 0,
    options: [],
    page: 1,
    zcpages: 1,
    onReachBottomNum: 0,
    grdata: [], //查询个人数据
    qxdata: [], //取消时的数据
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    await app.methods.getPhoneNumber();
    if (options.yl) {
      if (options.yl.length > 0) {
        this.setData({
          yl: Number(options.yl),
        });
      }
    }
    this.setData({
      options,
      onReachBottomNum: 0,
    });
    this.setData(await app.methods.myPhone()); // 获取本地存储手机号
    let grdata = {
      phone: this.data.myPhone,
      page: 1,
      limits: 20,
    };
    this.setData({
      grdata,
    });
    let obj = await this.getCollectPositions(grdata);
    if (obj.total != 0) {
      this.setData({
        cxlists: this.data.cxlists.concat(obj.result),
        chaxunjg: obj.total,
      });
    }
    // this.setData(this.getUser(grdata))
  },
  // 收藏/取消收藏
  collect: async function (e) {
    if (this.data.myPhone != "") {
      let { ggid, zwid, index, action } = e.currentTarget.dataset;
      xhs.showLoading({
        title: "取消收藏中！",
        icon: "none",
        duration: 2000,
      });
      let { cxlists, chaxunjg } = this.data;
      let data = {
        ggid,
        zwid,
        action,
        phone: this.data.myPhone,
      };
      let collectData = await this.updateCollect(data);
      xhs.hideLoading({});
      xhs.showToast({
        title: collectData == 1 ? "取消收藏成功!" : "取消收藏失败!",
        icon: "success",
        duration: 1500,
      });
      // 删除该条数据
      cxlists.splice(index, 1);
      chaxunjg -= 1;
      this.setData({
        cxlists,
        chaxunjg,
      });
    } else {
      xhs.showToast({
        title: "请先注册",
        duration: 2000,
      });
      if (app.globalData.PrivacyAuthorization) {
        this.setData({
          isShowAuth: !0,
          isPrivacyAuthorization: !0,
        });
      } else {
        this.setData({
          isShowAuth: !0,
          isPrivacyAuthorization: !1,
        });
      }
      return false;
    }
  },

  // 收藏和取消的方法
  updateCollect(data) {
    return new Promise((resolve) => {
      jiatuiRequest({
        url: "/api/v1/moonshot/collectPosition",
        method: "post",
        dataType: "jsonp",
        header: {
          "X-Requested-From": "WeChatMiniProgram",
        },
        data,
      })
        .then((res) => {
          if (res.code == 0) {
            resolve(1);
          } else {
            resolve(0);
          }
        })
        .catch((err) => {
          console.log(err);
          resolve(0);
        });
    });
  },

  // 获取收藏的职位
  getCollectPositions(data) {
    return new Promise((resolve) => {
      jiatuiRequest({
        url: "/api/v1/moonshot/getCollectPositions",
        method: "GET",
        dataType: "jsonp",
        header: {
          "X-Requested-From": "WeChatMiniProgram",
        },
        data,
      })
        .then((res) => {
          if (
            res.code == 0 &&
            res.data &&
            res.data.result &&
            res.data.result.length > 0
          ) {
            resolve(res.data);
          } else {
            resolve({ result: [], total: 0 });
          }
        })
        .catch((err) => {
          console.log(err);
          resolve({ result: [], total: 0 });
        });
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},
  // 回到顶部
  toTop() {
    xhs.pageScrollTo({
      scrollTop: 0,
    });
  },
  //去往查询
  goToCX: function () {
    var options = {
      lx: this.data.options.lx,
      actid: this.data.options.actid,
      xiangmu: this.data.options.xiangmu,
    };
    xhs.navigateBack({});
  },
  // 展开和收起
  toggle: function (e) {
    const { index } = e.currentTarget.dataset;
    // console.log(index)
    let { cxlists } = this.data;
    cxlists[index].toggle = !cxlists[index].toggle;
    this.setData({
      cxlists,
    });
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let { fx = 'hbwtb', gh = 'zcj37368', id, name } = this.data.options;
    console.log(`/packageB/zwjs/zwjs?id=${id}&fx=${fx}&gh=${gh}`)
    return {
      title: name,
      path: `/packageB/zwjs/zwjs?id=${id}&fx=${fx}&gh=${gh}`,
      imageUrl: "http://news01.offcn.com/hb/2025/0205/20250205025923173.jpg",
    };
  },
});
